package com.beermanager.activities;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.beermanager.R;
import com.doffman.dragarea.DragArea;
import com.doffman.dragarea.DragEvent;

/**
 * User: paulo
 * Date: 7/3/12
 * Time: 4:42 PM
 */
public class ManDrinkingDraggable extends ImageView {
    private DragArea mDragArea;
    private Drawable mMenImage;
    private Context mContext;
    
    private static int beerCount = 0;

    public ManDrinkingDraggable(Context context) {
        super(context);
        mContext = context;
        initDraggableMan();
    }

    public ManDrinkingDraggable(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDraggableMan();
    }

    public ManDrinkingDraggable(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initDraggableMan();
    }

    private void initDraggableMan(){
        setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.man_icon));
        mMenImage = getBackground();
    }
    
    public void setContext(Context context) {
    	this.mContext = context;
    }

    public void setDragArea(DragArea dragArea)
    {
        mDragArea = dragArea;

        mDragArea.addDragListener(ManDrinkingDraggable.this, new com.doffman.dragarea.OnDragListener()
        {
            @Override
            public void onDrag(View view, DragEvent dragEvent)
            {
                switch (dragEvent.getAction())
                {
                    case DragEvent.ACTION_DRAG_STARTED:
                        //ManDrinkingDraggable.this.setVisibility(INVISIBLE);
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DROP:
                    	if(beerCount++ < 5) {
                    		Toast.makeText(getContext(), "ceva" + beerCount, Toast.LENGTH_SHORT).show();
                    	} else {
                    		tooMuchBeer();
                    	}
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        //ManDrinkingDraggable.this.setVisibility(VISIBLE);
                        break;
                    default:
                        break;
                }
            }
        });
    }
    
    private void tooMuchBeer() {
		Builder builder = new Builder(mContext);
		builder.setMessage("Tu não acha que já bebeu demais?")
		       .setCancelable(false)
		       .setPositiveButton("Sim", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
              			Toast.makeText(getContext(), "Não vai pra casa dirigindo!", Toast.LENGTH_SHORT).show();
		                dialog.dismiss();
		           }
		       })
		       .setNegativeButton("Não", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.dismiss();
               			Toast.makeText(getContext(), "Procura o AA, para o teu bem!", Toast.LENGTH_SHORT).show();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
    }
    
    public static void resetBeerCount() {
    	beerCount = 0;
    }
}